package msu.erdc.hci;

import java.util.List;

import msu.erdc.hci.customObjects.AccidentObject;
import msu.erdc.hci.customObjects.ReportItemObject;
import msu.erdc.hci.customObjects.VehicleACObject;
import msu.erdc.hci.customObjects.PropertyObject;
import msu.erdc.hci.customObjects.PedestrianObject;
import msu.erdc.hci.customObjects.VehicleObject;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;


public class ReportList extends Fragment{
	
	View view;
	Button reportItemButton;
	boolean firstRun;
	AccidentObject accident;
	ListView vehicleList;
	ListView propertyList;
	ListView pedestrianList;
	TextView accidentCrumb;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
	        Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.report_list, null);
		return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //do stuff here
        
        Bundle arguments = getArguments();
        accident = ((Global)getActivity().getApplication()).accidents.get(arguments.getInt("accidentIndex"));
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		
		//Crumb
		accidentCrumb = (TextView) view.findViewById(R.id.accidentCrumbTextView);
		//Vehicles
		vehicleList = (ListView) view.findViewById(R.id.listVehicle);
		//Property
		propertyList = (ListView) view.findViewById(R.id.listProperty);
		//Pedestrian
		pedestrianList = (ListView) view.findViewById(R.id.listPedestrian);
				 
		ListAdapter vehicleAdapter = new ArrayAdapter<VehicleACObject>(getActivity(), android.R.layout.simple_list_item_1, accident.reportItemList.vehicleList);
		vehicleList.addHeaderView(createHeader(1));
		vehicleList.setAdapter(vehicleAdapter);
		 
		ListAdapter propertyAdapter = new ArrayAdapter<PropertyObject>(getActivity(), android.R.layout.simple_list_item_1, accident.reportItemList.propertyList);
		propertyList.addHeaderView(createHeader(2));
		propertyList.setAdapter(propertyAdapter);
		
		ListAdapter pedestrianAdapter = new ArrayAdapter<PedestrianObject>(getActivity(), android.R.layout.simple_list_item_1, accident.reportItemList.pedestrianList);
		pedestrianList.addHeaderView(createHeader(3));
		pedestrianList.setAdapter(pedestrianAdapter);
		
		vehicleList.setOnItemClickListener(vehicleListItemClick);
		propertyList.setOnItemClickListener(propertyListItemClick);
		pedestrianList.setOnItemClickListener(pedestrianListItemClick);
		
		accidentCrumb.setText(accident.toString() + "/Report Items: ");
	}
	
	@Override
	public void onDestroyView()
	{
		vehicleList.setAdapter(null);
		propertyList.setAdapter(null);
		pedestrianList.setAdapter(null);
		super.onDestroyView();
	}
	
	private View createHeader(int type)
	{
		LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	TextView tv = (TextView)inflater.inflate(android.R.layout.simple_list_item_1, null, true);
    	
    	if(type == 1)
    		tv.setText("+ ADD VEHICLE");
    	else if (type == 2)
    		tv.setText("+ ADD PROPERTY");
    	else
    		tv.setText("+ ADD PEDESTRIAN");
    	tv.setGravity(Gravity.CENTER);
    	return tv;
	}
	
	public OnItemClickListener vehicleListItemClick = new OnItemClickListener(){

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			
			int index = 0;
	        if(arg2>0)//an actual list item clicked
	        {
				index = arg2-1;
	        }
	        else//the ADD VEHICLE header was clicked
	        {
//	        	VehicleACObject newVehicle = new VehicleACObject();
//	        	List<VehicleACObject> vehicleList = accident.reportItemList.vehicleList;
//	        	vehicleList.add(newVehicle);
//	        	index = vehicleList.size()-1;
	        	//Adding an accident vehicle doesn't save so now its disabled
	        	Toast.makeText(getActivity(), "This feature is not implemented in this prototype", Toast.LENGTH_SHORT).show();
	        	return;
	        }
	        //start the vehicle fragment
	        FragmentTransaction ft = getFragmentManager().beginTransaction();
			AccidentVehicle vehicle = new AccidentVehicle();
			Bundle arguments = new Bundle();
			
			arguments.putInt("vehicleIndex", index);
			List<AccidentObject> temp = ((Global)getActivity().getApplication()).accidents;
			int i = temp.indexOf(accident);
			arguments.putInt("accidentIndex", i);
			
			vehicle.setArguments(arguments);
			ft.replace(android.R.id.content, vehicle, "vehicle");
			ft.addToBackStack(null);
			ft.commit();
			
			
		}};
		
	public OnItemClickListener propertyListItemClick = new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Toast.makeText(getActivity(), "This feature is not implemented in this prototype", Toast.LENGTH_SHORT).show();
		}};
	public OnItemClickListener pedestrianListItemClick = new OnItemClickListener(){

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
				Toast.makeText(getActivity(), "This feature is not implemented in this prototype", Toast.LENGTH_SHORT).show();
			
		}};
			
			
	public void onListItemClick(ListView l, View v, int position, long id) {
		int index = 0;
        if(position>0)//an actual list item clicked
        {
			index = position-1;
        }
        else//the ADD VEHICLE header was clicked
        {
        	//VehicleObject newVehicle = new VehicleObject();
        	//newVehicle.name = "New Vehicle";
        	//List<VehicleObject> vehicleList = ((Global)getActivity().getApplication()).vehicles;
        	//vehicleList.add(newVehicle);
        	//index = vehicleList.size()-1;
        }
        //start the vehicle fragment
        FragmentTransaction ft = getFragmentManager().beginTransaction();
		Vehicle vehicle = new Vehicle();
		Bundle arguments = new Bundle();
		arguments.putInt("vehicleIndex", index);
		vehicle.setArguments(arguments);
		ft.replace(android.R.id.content, vehicle, "vehicle");
		ft.addToBackStack(null);
		ft.commit();
    }
}
